import {createRouter,createWebHashHistory} from 'vue-router'
import userStore from '../store/user.js'
import {storeToRefs} from 'pinia'

const routes = [{
    path: "/",
    component: () => import('../view/Home.vue'),
    meta: {
      // 控制导航条是否显示
      headeAalive: true
    }
  },
  {
    path: "/register",
    component: () => import('../view/Register.vue')
  },
  {
    path: "/login",
    component: () => import('../view/Login.vue')
  },
  {
    path: "/wgInfo/:id",
    component: () => import('../view/WGInfo.vue'),
    meta: {
      // 控制导航条是否显示
      headeAalive: true
    }
  },
  {
    path: "/wgAdmin/:id",
    component: () => import('../view/WGAdmin.vue'),
    meta: {
      // 控制导航条是否显示
      headeAalive: true
    }
  }
]

const route = createRouter({
  history: createWebHashHistory(),
  routes
})

route.beforeEach((to, from, next) => {
  const uStore = userStore()
  if(to.path != "/login" && to.path != '/register') {
    // 判断是否保存了登录信息
    if(!uStore.getUser) {
      next("/login")
      return
    }
  }
  next()
})

export default route
